package com.xdtech.jmax.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.jmax.dao.PageViewDao;
import com.xdtech.jmax.model.PageView;
import com.xdtech.jmax.service.PageViewService;
import com.xdtech.jmax.vo.PageViewItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-03-12 10:24:48
 * @since 1.0
 * @see
 */
@Service
public class PageViewServiceImpl implements PageViewService {
	@Autowired
	private PageViewDao pageViewDao;

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-12 10:24:48
	 * @modified by
	 * @param entity
	 */
	public void save(PageView entity) {
		pageViewDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-12 10:24:48
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<PageView> entities) {
		pageViewDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-12 10:24:48
	 * @modified by
	 * @param entity
	 */
	public void delete(PageView entity) {
		pageViewDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-12 10:24:48
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		pageViewDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-12 10:24:48
	 * @modified by
	 * @param id
	 * @return
	 */
	public PageView get(Long id) {
		return pageViewDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-12 10:24:48
	 * @modified by
	 * @return
	 */
	public List<PageView> getAll() {
		return pageViewDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-12 10:24:48
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> pageViews = null;
		long rows = 0;
		if (pg!=null) {
			Page<PageView> page = pageViewDao.findPage(pg,"from PageView order by createTime desc", values);
			pageViews = BeanUtils.copyListProperties(
					PageViewItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<PageView> pageViewList = pageViewDao.find("from PageView order by id desc", values);
			pageViews = BeanUtils.copyListProperties(
					PageViewItem.class, pageViewList);
			rows = pageViews.size();
		}
		results.put("total", rows);
		results.put("rows", pageViews);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-12 10:24:48
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdatePageView(PageViewItem item) {
		PageView pageView = null;
		if (item.getId()==null) {
			pageView = new PageView();
		}else {
			pageView = pageViewDao.get(item.getId());
		}
		//复制前台修改的属�?
		BeanUtils.copyProperties(pageView, item);
		pageViewDao.save(pageView);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-12 10:24:48
	 * @modified by
	 * @param pageViewId
	 * @return
	 */
	public PageViewItem loadPageViewItem(Long pageViewId) {
		PageView pageView = pageViewDao.get(pageViewId);
		PageViewItem pageViewItem = new PageViewItem();
		BeanUtils.copyProperties(pageViewItem, pageView);
		return pageViewItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-12 10:24:48
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deletePageViewInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-12 10:24:48
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deletePageViewInfo(List<Long> pageViewIds) {
		for (Long id : pageViewIds) {
			delete(id);
		}
		return true;
	}
	/* (非 Javadoc)
	 * getJmaxPageCount
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param pageFlag
	 * @return
	 * @see com.xdtech.jmax.service.PageViewService#getJmaxPageCount(java.lang.String)
	 */
	@Override
	public Long getJmaxPageCount(String pageFlag)
	{
		List list = pageViewDao.findByHql("from PageView where formPage=?", pageFlag);
		return (long) list.size();
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:17:24
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		// TODO Auto-generated method stub
		return null;
	}

}
